package SwingView;

import javax.swing.*;

import SwingModel.CmsAnnouncement;
import SwingModel.CmsAssignment;
import SwingModel.CmsAssignmentSubmission;
import SwingModel.CmsCourse;
import SwingModel.CmsGradebook;
import SwingModel.CmsLecture;
import SwingModel.CmsMessage;
import SwingModel.CmsQuiz;
import SwingModel.CmsQuizSubmission;
import SwingModel.CmsStudent;
import SwingModel.CmsUser;
import SwingView.CmsFeaturePanel.FeatureButtons;
import SwingView.CmsPageSelector.CmsPage;
import SwingView.Lecture.ALLecturePost;

import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.util.List;


public class CmsMainView extends JFrame {

	private final CmsBannerPanel jpBanner;
	private final CmsFeaturePanel jpFeature;
	private final CmsAccountPanel	jpAccount;
	private final CmsContentPanel	jpContent;
	private final CmsUtilityPanel	jpUtility;
	
	
	
	//Card Layout:
	CardLayout cardLayout;
	
	public CmsMainView(String userFullName, String userType,List<CmsCourse> listOfCourse){
		//instantiate components
		jpBanner = new CmsBannerPanel();
		jpFeature = new CmsFeaturePanel();
		jpAccount = new CmsAccountPanel();
		jpContent = new CmsContentPanel();
		jpUtility = new CmsUtilityPanel();
		
		cardLayout = (CardLayout) jpContent.getLayout();
		
		//....................................................
		//SET UP MAIN VIEW GUI STRUCTURE:
		
		//set up HELPER PANEL 1
		JPanel jpHelper1 = new JPanel();
		jpHelper1.setLayout(new BorderLayout(0,0));
		jpHelper1.setOpaque(false);
		jpHelper1.add(jpAccount,BorderLayout.NORTH);
		jpHelper1.add(jpContent,BorderLayout.CENTER);
		jpHelper1.add(jpUtility,BorderLayout.EAST);
		
		//set up View Frame:
		getContentPane().setBackground(new Color(20,20,20)); //set background color to be dark grey
		setSize(new Dimension(1024,700)); //default size to be 800x600 by pixel
		setMinimumSize(new Dimension(1024,600));
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //terminate the program when close
		setLocationRelativeTo(null); //Default location in the center of the screen
		setLayout(new GridBagLayout());
		
		//Central Panel
		JPanel jpCentral = new JPanel();
		jpCentral.setLayout(new BorderLayout(0,0)); //set Border Layout with 0 hgap and vgap
		jpCentral.setPreferredSize(new Dimension(1024,700));
		jpCentral.setMinimumSize(new Dimension(1024,700));
		jpCentral.setBackground(new Color(41,41,41));
		jpCentral.setOpaque(true);
		
		//add components:
		jpCentral.add(getJpBannerPanel(),BorderLayout.NORTH);
		jpCentral.add(jpFeature,BorderLayout.WEST);
		jpCentral.add(jpHelper1,BorderLayout.CENTER);

		//add central panel to frame
		GridBagConstraints c = new GridBagConstraints();
		c.weighty = 1.0;
		c.weightx = 1.0;
		c.fill = GridBagConstraints.VERTICAL;
		c.anchor = GridBagConstraints.CENTER;
		add(jpCentral,c); 
		//....................................................

		//....................................................
		//UPDATE THE ACCOUNT PANEL TO DISPLAY THE MAIN VIEW:
		//update user full name on main view:
		jpAccount.getJlUserName().setText(userFullName);
		jpAccount.getJlUserName().revalidate();
		
		//update user role on main view:
		jpAccount.getJlUserType().setText(userType + ": ");
		jpAccount.getJlUserType().revalidate();
		
		//update the comboBox
		jpAccount.getJcbCourseList().addItem("Select Course"); //Add default first item as "Select Course"
		for(CmsCourse course : listOfCourse){ //add all courses to the list
			jpAccount.getJcbCourseList().addItem(course.getCourseID() + " - "+course.getCourseTitle());
		}
		jpAccount.getJcbCourseList().revalidate(); //Revalidates the ComboBox to update data on it
		jpAccount.revalidate();//Revalidates the AccountPanel to update data on it
		//....................................................
		
		
		//Set up main frame to display the home page:
		displayHomePage();
		
	}

	//---------------------------------------------------------
	//Display home page
	//---------------------------------------------------------		
	public void displayHomePage(){
		
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Set original default image icon for all button
		jpFeature.disableFeatureButtonPanel();//hide the feature buttons
		
		//set content panel to show the home page
		cardLayout.show(jpContent, CmsContentPanel.CARD_HOME_PAGE);
	}
	
	//---------------------------------------------------------
	//Display course page
	//---------------------------------------------------------		
	public void displayCoursePage(String courseID){
		
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		
		//set content panel to show the home page
		cardLayout.show(jpContent, CmsContentPanel.CARD_HOME_PAGE);
		
		//update the course title on the Feature panel
		jpFeature.setCourseTitle(courseID);
	}
	
	//---------------------------------------------------------
	//Display people page
	//---------------------------------------------------------		
	public void displayPeoplePage(List<CmsUser> listOfPeople){
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.PEOPLE);
		
		//set content panel to show the home page
		cardLayout.show(jpContent, CmsContentPanel.CARD_PEOPLE_PAGE);
		
		//set people data for the People Page
		jpContent.getPeoplePage().setPeopleData(listOfPeople);
	}	
	
	
	//---------------------------------------------------------
	//Display instructor lecture page
	//---------------------------------------------------------		
	public void displayLectureInstructorPage(List<CmsLecture> listOfLecture){
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.LECTURE);
		
		//set content panel to show the home page
		cardLayout.show(jpContent, CmsContentPanel.CARD_INSTRUCTOR_LECTURE_PAGE);
		
		//set people data for the People Page
		jpContent.getLectureInstructorPage().displayListOfLecture(listOfLecture);;
	}	
	
	//---------------------------------------------------------
	//Display student lecture page
	//---------------------------------------------------------		
	public void displayLectureStudentPage(List<CmsLecture> listOfLecture){
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.LECTURE);
		
		//set content panel to show the home page
		cardLayout.show(jpContent, CmsContentPanel.CARD_STUDENT_LECTURE_PAGE);
		
		//set people data for the People Page
		jpContent.getLectureStudentPage().displayListOfLecture(listOfLecture);;
	}	
	
	//---------------------------------------------------------
	//Display instructor ANNOUNCEMENT page
	//---------------------------------------------------------		
	public void displayAnnouncementInstructorPage(List<CmsAnnouncement> listOfAnnouncements){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.ANNOUNCEMENT);
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_INSTRUCTOR_ANNOUNCEMENT_PAGE);
		
		//set people data for the Announcement Page
		jpContent.getAnmInstructorPage().displayListOfAnnouncements(listOfAnnouncements);;
	}	
	
	//---------------------------------------------------------
	//Display student ANNOUNCEMENT page
	//---------------------------------------------------------		
	public void displayAnnouncementStudentPage(List<CmsAnnouncement> listOfAnnouncements){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.ANNOUNCEMENT);
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_STUDENT_ANNOUNCEMENT_PAGE);
		
		//set people data for the Announcement Page
		jpContent.getAnmStudentPage().displayListOfAnnouncements(listOfAnnouncements);;
	}
	
	//---------------------------------------------------------
	//Display instructor QUIZ page
	//---------------------------------------------------------		
	public void displayQuizInstructorPage(List<CmsQuiz> listOfQuiz){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.QUIZ);
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_QUIZ_MAIN_INSTRUCTOR_PAGE);
	 
		System.out.println("HERE INSIDE DISPLAY QUIZ MAIN INSTRUCTOR");
		
		//set people data for the Announcement Page
		jpContent.getQuizMainInstructorPage().displayListOfQuiz(listOfQuiz);
	}	
	
	//---------------------------------------------------------
	//Display student QUIZ page
	//---------------------------------------------------------		
	public void displayQuizStudentPage(List<CmsQuiz> listOfQuiz){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.QUIZ);
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_QUIZ_MAIN_STUDENT_PAGE);
		
		//set people data for the Announcement Page
		jpContent.getQuizMainStudentPage().displayListOfQuiz(listOfQuiz);
	}
	
	//---------------------------------------------------------
	//Display QUIZ COMPOSER page
	//---------------------------------------------------------		
	public void displayQuizComposerPage(){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.QUIZ);
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_QUIZ_COMPOSER_PAGE);
		System.out.println("HERE INSIDE DISPLAY QUIZ COMPOSER");
		jpContent.getQuizComposerPage().clearAllInput();
	}
	
	//---------------------------------------------------------
	//Display QUIZ EDIT page
	//---------------------------------------------------------		
	public void displayQuizEditPage(CmsQuiz quiz){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.QUIZ);
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_QUIZ_EDIT_PAGE);
		
		jpContent.getQuizEditPage().loadQuiz(quiz);
	}
	
	//---------------------------------------------------------
	//Display QUIZ SUBMISSION page
	//---------------------------------------------------------		
	public void displayQuizSubmissionPage(CmsQuiz quiz, List<CmsQuizSubmission> quizSubs, List<CmsStudent> students){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.QUIZ);
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_QUIZ_SUBMISSIONS_PAGE);
		
		jpContent.getQuizSubmissionsPage().loadQuiz(quiz, quizSubs, students);
	}
	
	//---------------------------------------------------------
	//Display QUIZ EDIT page
	//---------------------------------------------------------		
	public void displayQuizTakePage(CmsQuiz quiz){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.QUIZ);
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_QUIZ_TAKE_PAGE);
		
		jpContent.getQuizTakePage().loadQuiz(quiz);
	}
	
	//---------------------------------------------------------
	//Display QUIZ EDIT page
	//---------------------------------------------------------		
	public void displayQuizViewPage(CmsQuiz quiz){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.QUIZ);
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_QUIZ_VIEW_PAGE);
		
		jpContent.getQuizViewPage().loadQuiz(quiz);
	}
	
	//---------------------------------------------------------
	//Display QUIZ EDIT page
	//---------------------------------------------------------		
	public void displayQuizResultPage(CmsQuiz quiz, CmsQuizSubmission quizSub){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.QUIZ);
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_QUIZ_RESULT_PAGE);
		
		jpContent.getQuizResultPage().loadQuiz(quiz, quizSub);
	}
	
	
	
	//---------------------------------------------------------
	//Display Message page
	//---------------------------------------------------------		
	public void displayMessagePage(List<CmsMessage> listOfMessage){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.MESSAGE);
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_MESSAGE_PAGE);
		
		//TODO: 
		//set people data for the Announcement Page
		jpContent.getMessagePage().displayListOfMessages(listOfMessage);;
	}
	
	//---------------------------------------------------------
	//Display instructor GRADEBOOK page
	//---------------------------------------------------------		
	public void displayGradebookInstructorPage(CmsGradebook gradebook){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.GRADEBOOK);
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_INSTRUCTOR_GRADEBOOK_PAGE);
		
		//set people data for the Announcement Page
		jpContent.getGbInstructorPage().loadGradebook(gradebook);
	}		
	
	//---------------------------------------------------------
	//Display student GRADEBOOK page
	//---------------------------------------------------------		
	public void displayGradebookStudentPage(CmsGradebook gradebook){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.GRADEBOOK);
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_STUDENT_GRADEBOOK_PAGE);
		
		 
		//set people data for the Announcement Page
		jpContent.getGbStudentPage().loadGradebook(gradebook);
	}	
	
	//---------------------------------------------------------
	//Display instructor ASSIGNMENT page
	//---------------------------------------------------------		
	public void displayAssignmentInstructorPage(List<CmsAssignment> listOfAssignment){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.ASSIGNMENT);
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_ASGM_MAIN_INSTRUCTOR_PAGE);

		//set people data for the Announcement Page
		jpContent.getAsgmMainInstructorPage().displayListOfAssignment(listOfAssignment);
	}
	
	//---------------------------------------------------------
	//Display instructor ASSIGNMENT COMPOSER page
	//---------------------------------------------------------		
	public void displayAssignmentComposerPage(){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.ASSIGNMENT);
		
		jpContent.getAsgmComposerPage().clearAllInput();
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_ASGM_COMPOSER_PAGE);
		
		
		
	}
	
	//---------------------------------------------------------
	//Display instructor ASSIGNMENT VIEW page
	//---------------------------------------------------------		
	public void displayAssignmentViewInstructorPage( CmsAssignment asgm){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.ASSIGNMENT);
		
		jpContent.getAsgmViewInstructorPage().loadViewingAsgm(asgm);
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_ASGM_VIEW_INSTRUCTOR_PAGE);
		
	}
	
	//---------------------------------------------------------
	//Display instructor ASSIGNMENT EDIT page
	//---------------------------------------------------------		
	public void displayAssignmentEditPage( CmsAssignment asgm){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.ASSIGNMENT);
		
		jpContent.getAsgmEditPage().loadEdittingAsgm(asgm);
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_ASGM_EDIT_PAGE);
		
	}
	
	//---------------------------------------------------------
	//Display instructor ASSIGNMENT EDIT page
	//---------------------------------------------------------		
	public void displayAssignmentSubmissionViewPage( CmsAssignment asgm, List<CmsAssignmentSubmission> asgmSubmissions, List<CmsStudent> students){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.ASSIGNMENT);
		
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_ASGM_SUBMISSION_VIEW_PAGE);
		
		jpContent.getAsgmSubmissionPage().loadViewingAsgm(asgm, asgmSubmissions, students);
		
	}
	
	
	//---------------------------------------------------------
	//Display student ASSIGNMENT page
	//---------------------------------------------------------		
	public void displayAssignmentMainStudentPage(List<CmsAssignment> listOfAssignment){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.ASSIGNMENT);
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_ASGM_MAIN_STUDENT_PAGE);
		
		//set people data for the Announcement Page
		jpContent.getAsgmMainStudentPage().displayListOfAssignment(listOfAssignment);
	}
	
	//---------------------------------------------------------
	//Display student ASSIGNMENT VIEW/SUBMIT page
	//---------------------------------------------------------		
	public void displayAssignmentViewStudentPage(CmsAssignment asgm, CmsAssignmentSubmission asgmSubmission){
		
		//select and highlight the announcement button
		jpFeature.setEnableAllButton();//Enable all button
		jpFeature.setOriginalImageIconForButtons();//Unselect all buttons
		jpFeature.enableFeatureButtonPanel();//display feature buttons panel
		jpFeature.setSelectedButton(FeatureButtons.ASSIGNMENT);
		
		//set content panel to show the announcement instructor page
		cardLayout.show(jpContent, CmsContentPanel.CARD_ASGM_VIEW_STUDENT_PAGE);
		
		//set people data for the Announcement Page
		jpContent.getAsgmViewStudentPage().loadViewingAsgm(asgm,asgmSubmission);
	}	
	

	
	//---------------------------------------------------------
	//GETTERS:
	//---------------------------------------------------------	
	public CmsBannerPanel getJpBannerPanel() {
		return jpBanner;
	}
	
	public CmsAccountPanel getAccountPanel(){
		return jpAccount;
	}

	public CmsFeaturePanel getFeaturePanel(){
		return jpFeature;
	}

	public CmsContentPanel getContentPanel(){
		return jpContent;
	}

    public JButton getHomeButton(){
        return jpAccount.getJbtHome();
	}
	
	public JButton getAnnouncementButton(){
	        return jpFeature.getJbtAnnouncements();
	}
	
	public JButton getMessageButton(){
	        return jpFeature.getJbtMessages();
	}
	
	public JButton getQuizButton(){
	        return jpFeature.getJbtQuizzes();
	}
	
	public JButton getAssignmentButton(){
	        return jpFeature.getJbtAssignments();
	}
	
	public JButton getPeopleButton(){
	        return jpFeature.getJbtPeople();
	}
	
	public JButton getGradebookButton(){
	        return jpFeature.getJbtGradebook();
	}
	
	public JButton getLectureMaterialButton(){
	        return jpFeature.getJbtLectures();
	}
}
